#include <stdio.h>
#include <stdlib.h>
#include "Menu.h"
#include "SdlJeu.h"
#include <assert.h>
const int MENU_MAX = 4;

void Instruction(SDL_Surface *ecran, TTF_Font *Mario,TTF_Font *Snorlax,
                  SDL_Color Noir, SDL_Color Rouge)
{

    SDL_Surface *ligne[7] = {NULL}, *titre = NULL, *caption = NULL;
    SDL_Rect posLigne[7], posTitre, posCaption;
    SDL_Event instructions;
    long i = 0;
    int continuer = 1;
    titre = TTF_RenderText_Blended(Mario, "Instructions", Rouge);
    posTitre.x = ecran->w / 2 - titre->w / 2;
    posTitre.y = 10;
    ligne[0] = TTF_RenderText_Blended(Snorlax, "A l'aide de la fleche directionnelle haut,", Noir);
    posLigne[0].x = 35;
    posLigne[0].y = 140;
    ligne[1] = TTF_RenderText_Blended(Snorlax, "deplacer votre personnage afin qu'il evites ", Noir);
    posLigne[1].x = 35;
    posLigne[1].y = 170;
    ligne[2] = TTF_RenderText_Blended(Snorlax, "les obstacles et aider le a parcourir", Noir);
    posLigne[2].x = 35;
    posLigne[2].y = 200;
    ligne[3] = TTF_RenderText_Blended(Snorlax, "la plus longue distance. ", Noir);
    posLigne[3].x = 35;
    posLigne[3].y = 230;
    caption = TTF_RenderText_Blended(Snorlax, "[R] Retour", Noir);
    posCaption.x = 79;
    posCaption.y = 350;
    SDL_BlitSurface(titre, NULL, ecran, &posTitre);
    for(i=0;i<5;i++)
    SDL_BlitSurface(ligne[i], NULL, ecran, &posLigne[i]);
    SDL_BlitSurface(caption, NULL, ecran, &posCaption);
    SDL_Flip(ecran);
    while (continuer)
    {
        SDL_WaitEvent(&instructions);
        switch(instructions.type)
        {
            case SDL_QUIT:
            continuer = 0;
            exit(EXIT_SUCCESS);
            break;
            case SDL_KEYDOWN:
            switch(instructions.key.keysym.sym)
            {
                case SDLK_r:
                    default :
                    SDL_FreeSurface(titre);
                    SDL_FreeSurface(caption);
                    for(i = 0; i < 6; i++)
                        SDL_FreeSurface(ligne[i]);
                    main(1, "Insctructions");
                    break;
                case SDLK_ESCAPE:
                    continuer = 0;
                    SDL_FreeSurface(titre);
                    SDL_FreeSurface(caption);
                    for(i = 0; i < 6; i++)
                        SDL_FreeSurface(ligne[i]);
                    exit(EXIT_SUCCESS);
                    break;
             }
        }
    }
}

void SDL()
{
    sdlJeu Sj;
    sdlInit(&Sj);
	sdljeuBoucle(&Sj);
	sdlLibere(&Sj);
}

void score(SDL_Surface *ecran, TTF_Font *Mario,TTF_Font *Snorlax,
                  SDL_Color Noir, SDL_Color Rouge)
{

    int i,j=100,continuer = 1;
    SDL_Surface *score,*titre = NULL,*caption = NULL;
    SDL_Rect  posScore[10],posTitre,posCaption;
    SDL_Event instructions;
    int tabscore[10] = {0};
    char  score2[50] = {0};
    FILE * scoreFic;
    scoreFic = fopen("data/Score.txt", "r");
    if (scoreFic==NULL)
    {
        printf("Erreur lors de l'ouverture de %s\n", "data/Score.txt");
        assert(scoreFic);
    }


            titre = TTF_RenderText_Blended(Mario, "Scores", Rouge);
            posTitre.x = ecran->w / 2 - titre->w / 2;
            posTitre.y = 10;
            fscanf (scoreFic, "%d" "%d" "%d" "%d" "%d" "%d" "%d" "%d" "%d" "%d" , &tabscore[0],&tabscore[1],&tabscore[2],
                    &tabscore[3],&tabscore[4],&tabscore[5],&tabscore[6],&tabscore[7],&tabscore[8],&tabscore[9]);

            caption = TTF_RenderText_Blended(Snorlax, "[R] Retour", Noir);
            fclose(scoreFic);
            posCaption.x = 230;
            posCaption.y = 430;
            SDL_BlitSurface(titre, NULL, ecran, &posTitre);
            SDL_BlitSurface(caption, NULL, ecran, &posCaption);

            for(i=0;i<10;i++)
            {
                posScore[i].x = 230;
                posScore[i].y= j;
                j=j+30;
            }


            for(i=0;i<10;i++)
            {

                sprintf(score2,"#%d   " "%d",i+1,tabscore[9-i]);
                score = TTF_RenderText_Blended(Snorlax,score2,Noir);

                    SDL_BlitSurface(score, NULL,ecran, &posScore[i]);


                SDL_Flip(ecran);
            }
            while (continuer)
            {
                SDL_WaitEvent(&instructions);
                switch(instructions.type)
                {
                    case SDL_QUIT:
                    continuer = 0;
                    exit(EXIT_SUCCESS);
                    break;
                    case SDL_KEYDOWN:
                    switch(instructions.key.keysym.sym)
                    {
                        case SDLK_r:
                        default :
                        SDL_FreeSurface(titre);
                        SDL_FreeSurface(score);
                        SDL_FreeSurface(caption);
                        main(1, "Scores");
                        break;
                        case SDLK_ESCAPE:
                        SDL_FreeSurface(titre);
                        SDL_FreeSurface(score);
                        SDL_FreeSurface(caption);
                        continuer = 0;
                        exit(EXIT_SUCCESS);
                        break;
                     }
                }
            }

}

void menuAff(SDL_Surface *ecran, SDL_Surface *fond,TTF_Font *Mario,TTF_Font *Snorlax
             , SDL_Color Noir, SDL_Color Rouge)
{
    int i;
    SDL_Init(SDL_INIT_VIDEO);
    SDL_Surface *bienvenue, *select[4]={NULL};
    bienvenue=NULL;
    SDL_Rect posBienvenue, posSelect[4];

    SDL_Event event;
    int continuer = 1;

    bienvenue = TTF_RenderText_Blended(Mario, "MarioJetpack", Rouge);
    select[0] = TTF_RenderText_Blended(Snorlax, "[J] Jouer", Noir);
    posSelect[0].x = 440;
    posSelect[0].y = 260;
    select[1] = TTF_RenderText_Blended(Snorlax, "[I] Instruction", Noir);
    posSelect[1].x = 440;
    posSelect[1].y = 285;
    select[2] = TTF_RenderText_Blended(Snorlax, "[S] Scores", Noir);
    posSelect[2].x = 440;
    posSelect[2].y = 310;
    select[3] = TTF_RenderText_Blended(Snorlax, "[ECH] Quitter", Noir);
    posSelect[3].x = 440;
    posSelect[3].y = 335;
    SDL_Rect posFond;
    posFond.x = 0;
    posFond.y = 0;
    SDL_WM_SetIcon(SDL_LoadBMP("icon.bmp"), NULL);
    ecran = SDL_SetVideoMode(650, 480, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
    posBienvenue.x = ecran->w / 2 - bienvenue->w/2;
    posBienvenue.y = 10;

    SDL_WM_SetCaption("Le Jeu JetPack", NULL);
    fond = SDL_LoadBMP("data/fond.bmp");
    SDL_BlitSurface(fond, NULL, ecran, &posFond);
    SDL_BlitSurface(bienvenue, NULL, ecran, &posBienvenue);
    for(i=0; i<4; i++)
    {
        SDL_BlitSurface(select[i], NULL, ecran, &posSelect[i]);
    }

    SDL_Flip(ecran);
	SDL_Delay(5);

    for(i = 0; i < 4; i++)
        SDL_FreeSurface(select[i]);

    while (continuer)
    {
        SDL_WaitEvent(&event);
        switch(event.type)
        {
        case SDL_QUIT:
        continuer = 0;
        SDL_FreeSurface(bienvenue);
        SDL_FreeSurface(fond);
        exit(EXIT_SUCCESS);
        break;
        case SDL_KEYDOWN:
        switch(event.key.keysym.sym)
            {
            case SDLK_i:
            SDL_BlitSurface(fond, NULL, ecran, &posFond);
            SDL_FreeSurface(bienvenue);
            SDL_FreeSurface(fond);
            Instruction(ecran,Mario,Snorlax,Noir,Rouge);
            SDL_Quit();
            default :
            break;
            case SDLK_j:
            SDL_FreeSurface(bienvenue);
            SDL_FreeSurface(fond);
            SDL();
            SDL_Quit();
            break;
            case SDLK_s:
            SDL_BlitSurface(fond, NULL, ecran, &posFond);
            SDL_FreeSurface(bienvenue);
            SDL_FreeSurface(fond);
            score(ecran,Mario,Snorlax,Noir,Rouge);
            SDL_Quit();
            break;
            case SDLK_ESCAPE:
            continuer = 0;
            SDL_FreeSurface(bienvenue);
            SDL_FreeSurface(fond);
            SDL_Quit();
            exit(EXIT_SUCCESS);
            break;
            }
        }
    }


}

ChoixMenu menuQuestion()
{
	ChoixMenu cm;
	int i;
	char dum[32];
	int ok=0;
	do
	{
		if (scanf("%d",&i)!=1) scanf("%s",dum);
		cm=((ChoixMenu)(i));
		if ((cm<0) || (cm>=MENU_MAX))
			printf("Erreur choix menu\n");
		else ok=1;
		fflush(stdout);
	} while(ok==0);
	printf("\n"); fflush(stdout);
	return cm;



}

